<?php namespace Common\Model;

use Hdphp\Model\Model;
/**
 * 商品列表模型
 */
class GoodsList extends Model
{
	//操作数据表
	protected $table = "goods_list";
	//自动验证
	protected $validate = array(
		array('inventory','required','库存不能为空',3,3),
		array(),
		array(),
	);
	
	/**
	 * 添加
	 */
	public function store()
	{
		//自动验证
		if(!$this->create()) return false;
		
		$gid = Q('get.gid',0,'intval');
		//把spec[]提交的数组转化成字符串存入数据库
		$spec = implode("|", $_POST['spec']);
		//$spec为两个id
		/*$re = $this->where('combine','=',$spec)->first();
		if($re){
			$this->error = "该属性已添加";
			return false;
		}*/
		
		
		$data = array(
			'combine'=>$spec,
			'goods_gid'=>$gid,
			'inventory'=>$_POST['inventory'],
		);
		
		if($_POST['number'])
		{
			$data['number'] = $_POST['number'];
		}else{
			$data['number'] = time().rand(0, 1000);
		}
		//echo 1 ;die;
		$number = $this->where('number','=',$data['number'])->get();
		//p($number);
		if($number){
			$this->error = "该货品货号已存在";
			return false;
		}
		//执行添加
		p($data);
		//echo 1 ;die;
		return $this->add($data);
	}
	/**
	 * 编辑
	 */
	public function edit($glid)
	{
		//自动验证
		if(!$this->create()) return false;
		//压入数组数据
		$data = array(
			'combine'=>implode("|", $_POST['spec']),
			'goods_gid'=>Db::table('goods_list')->where('glid','=',$glid)->pluck('goods_gid'),
			'inventory'=>$_POST['inventory'],
			'number'=>$_POST['number'],
		);
		//执行更改
		$this->where('glid','=',$glid)->update($data);
		return true;
	}
	/**
	 * 删除
	 */
	public function del($glid)
	{
		//执行删除
		$this->where('glid','=',$glid)->delete();
	}
}
